package com.ruicar.afs.cloud.basic.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.basic.common.entity.BasicReceiptDealDetails;
import com.ruicar.afs.cloud.basic.common.mapper.BasicReceiptDealDetailsMapper;
import com.ruicar.afs.cloud.basic.common.service.BasicExpenseRecordService;
import com.ruicar.afs.cloud.basic.common.service.BasicReceiptDealDetailsService;
import com.ruicar.afs.cloud.basic.common.service.BasicRepaymentPlanService;
import com.ruicar.afs.cloud.batch.entity.AfsBatchInfo;
import com.ruicar.afs.cloud.common.modules.contract.enums.FeeTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ReceiptStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Lee
 * @date 2020-05-30 04:35:07 
 * @description 
 */
@Service
@AllArgsConstructor
public class BasicReceiptDealDetailsServiceImpl extends ServiceImpl<BasicReceiptDealDetailsMapper, BasicReceiptDealDetails> implements BasicReceiptDealDetailsService {

    private final BasicRepaymentPlanService basicRepaymentPlanService;

    private final BasicExpenseRecordService basicExpenseRecordService;

    @Override
    public List<BasicReceiptDealDetails> handleReceiptDetailForCancel(Long receiptId, AfsBatchInfo afsBatchInfo, Date batchDate) {
        List<BasicReceiptDealDetails> basicReceiptDealDetailsList = this.list(Wrappers.<BasicReceiptDealDetails>query().lambda()
                .eq(BasicReceiptDealDetails::getReceiptId,receiptId)
        );
        if(EmptyUtils.isNotEmpty(basicReceiptDealDetailsList)){
            for (BasicReceiptDealDetails receiptDealDetails:basicReceiptDealDetailsList){
                receiptDealDetails.setStatus(ReceiptStatusEnum.revoke);

                /**  本金 by ZC.GUO  **/
                if (FeeTypeEnum.capital.equals(receiptDealDetails.getReceiptType())){
                    basicRepaymentPlanService.handleCapitalForCancel(receiptDealDetails,afsBatchInfo,batchDate);
                }
                /**  利息 by ZC.GUO  **/
                else if(FeeTypeEnum.interest.equals(receiptDealDetails.getReceiptType())){
                    basicRepaymentPlanService.handleInterestForCancel(receiptDealDetails,afsBatchInfo,batchDate);
                }
                /**  费用、罚息相关处理 by ZC.GUO  **/
                else{
                    basicExpenseRecordService.cancelExpenseRecord(receiptDealDetails);
                }
                this.updateById(receiptDealDetails);
            }
        }
        return basicReceiptDealDetailsList;
    }
}
